from flask import request, jsonify, Blueprint
from flaskapp.util.Redprint import Redprint
from flaskapp.entity.House import House
from flaskapp.entity.User import User

api = Redprint('houses')


@api.route('/housesList', methods=['GET'])
def getHouses():
    # 获取查询参数
    keyword = request.args.get('keyword', '')
    district = request.args.get('district')
    min_rent = request.args.get('min_rent', type=float)
    max_rent = request.args.get('max_rent', type=float)
    rental_method = request.args.get('rental_method')
    orientation = request.args.get('orientation')
    status = request.args.get('status')
    house_type = request.args.get('house_type')  # 户型参数
    decoration = request.args.get('decoration')  # 添加装修参数

    # 添加排序参数
    sort_by = request.args.get('sort_by')
    order = request.args.get('order', 'asc')

    query = House.query

    # 关键词搜索
    if keyword:
        query = query.filter(House.description.like(f'%{keyword}%'))

    # 分类筛选
    if district:
        query = query.filter(House.district == district)
    if min_rent is not None:
        query = query.filter(House.monthly_rent >= min_rent)
    if max_rent is not None:
        query = query.filter(House.monthly_rent <= max_rent)
    if rental_method:
        query = query.filter(House.rental_method == rental_method)
    if orientation:
        query = query.filter(House.orientation == orientation)
    if status:
        query = query.filter(House.status == status)
    if house_type:
        query = query.filter(House.house_type.like(f'%{house_type}%'))
    if decoration:  # 添加装修筛选条件
        query = query.filter(House.decoration == decoration)

    # 添加排序逻辑
    if sort_by == 'price':
        if order == 'asc':
            query = query.order_by(House.monthly_rent.asc())
        else:
            query = query.order_by(House.monthly_rent.desc())
    elif sort_by == 'area':
        if order == 'asc':
            query = query.order_by(House.floor_area.asc())
        else:
            query = query.order_by(House.floor_area.desc())

    houses = query.all()
    result = []
    for house in houses:
        result.append({
            'house_id': house.house_id,
            'description': house.description,
            'photo_url': house.photo_url,
            'monthly_rent': float(house.monthly_rent),
            'rental_method': house.rental_method,
            'house_type': house.house_type,
            'floor_area': float(house.floor_area),
            'orientation': house.orientation,
            'decoration': house.decoration,
            'district': house.district,
            'address': house.address,
            'landlord_id': house.landlord_id,
            'status': house.status
        })
    return jsonify(result)


@api.route('/getHouseDetails/<houseid>', methods=['GET'])
def getHouseDetails(houseid):
    house = House.query.filter_by(house_id=houseid).first_or_404()

     # 根据 landlord_id 查询用户
    landlord = User.query.filter_by(user_id=house.landlord_id).first()
    account_name = landlord.account_name if landlord else '未知用户'
    landlord_phone = landlord.phone if landlord else '未知电话'

    return jsonify({
        'house_id': house.house_id,
        'description': house.description,
        'photo_url': house.photo_url,
        'monthly_rent': float(house.monthly_rent),
        'rental_method': house.rental_method,
        'house_type': house.house_type,
        'floor_area': float(house.floor_area),
        'orientation': house.orientation,
        'decoration': house.decoration,
        'district': house.district,
        'address': house.address,
        'landlord_id': house.landlord_id,
        'status': house.status,
        'landlord_name': account_name,
        'landlord_phone': landlord_phone
        # 前端可根据house_id和landlord_id实现"与房东聊天"与"收藏”按钮
    })